package com.hnhczh.alipaydemo.demos.web.controller;


import com.hnhczh.alipaydemo.demos.web.entity.AliPayParam;
import com.hnhczh.alipaydemo.demos.web.entity.R;
import com.hnhczh.alipaydemo.demos.web.service.ZfbService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 支付宝相关接口
 */
@Api(tags = "支付宝相关接口")
@RequestMapping("/zfb")
@RestController
public class ZfbController {

    @Resource
    private ZfbService zfbService;

    /**
     * 支付宝手机网站支付
     *
     * @param param
     */
    @PostMapping(value = "/orderPay")
    public R<String> orderPay(@RequestBody AliPayParam aliPayParam) {
        try {
            return R.ok(zfbService.orderPay(aliPayParam));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 支付宝订单支付回调
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/payNotify")
    public R<String> payNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            return R.ok(zfbService.orderPayNotify(request, response));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 支付宝查询订单结果
     *
     * @param param
     */
    @PostMapping(value = "/searchOrder")
    public R<String> searchOrder(@RequestBody AliPayParam aliPayParam) {
        try {
            return zfbService.searchOrder(aliPayParam.getOrderId());
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 支付宝查询订单结果
     *
     * @param param
     */
    @PostMapping(value = "/downoloadOrder")
    public R<String> downoloadOrder(HttpServletResponse response) {
        try {
            return zfbService.downloadOrder(response);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    //
//    /**
//     * 支付宝订单支付退款
//     */
//    @RequestMapping(value = "/refund")
//    public R<Boolean> refund(@RequestBody Map<String, Object> param) {
//        try {
//            zfbService.refund(param);
//            return R.ok(true);
//        } catch (Exception e) {
//            return R.fail(e.getMessage());
//        }
//    }
//
    @RequestMapping(value = "/testOrderPay")
    public void testOrderPay(HttpServletResponse response) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("orderId", UUID.randomUUID().toString());
            param.put("goodsName", "愚钝的愚钝");
            param.put("fee", 0.01);
            zfbService.testOrderPay(param, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
